// components/sexSelector/sexSelector.js
var util = require('../../utils/util.js')
var _this
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    avatarUrl: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {
        _this = this
        if (newVal.trim() != oldVal.trim()) {
        }
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    sex:0,
    sureBtnStyle: 'btnNormal',
    cancelBtnStyle: 'btnNormal',
    filePath: ''

  },

  /**
   * 组件的方法列表
   */
  methods: {
    manBtnClick: function (e) {
      this.setData({
        sureBtnStyle: 'btnSelected',
        cancelBtnStyle: 'btnNormal',
        sex:1
      })
      this.triggerEvent('sexFinishedHandle', {'sex':1}, {})
    },
    womenBtnClick: function (e) {
      this.setData({
        sureBtnStyle: 'btnNormal',
        cancelBtnStyle: 'btnSelected',
        sex:2
      })
      this.triggerEvent('sexFinishedHandle', { 'sex': 2 }, {})
    },
    nextBtnClick: function (e) {
      if (_this.data.sex == 0) {
        wx.showToast({
          title:  '亲，性别还是很重要哟',
          icon: 'none'
        })
        return 
      }
      this.triggerEvent('goNext', {}, {})
    },
    chooseImage: () => {
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths)
          _this.setData({
            filePath: tempFilePaths
          })
          util.fileUpload(_this.requestDidResponse, _this.requestDidFaild, _this.data.filePath[0])
          wx.showLoading({
            title: '正在设置...',
          })
        },
        fail() {

        }
      })
    },
    requestDidResponse: (res) => {
      wx.hideLoading()
      var data = JSON.parse(res)
      if (data.status == '200') {
        _this.triggerEvent('avatarChangeSuccessHandle', { 'imgUrl': data.data.imgUrl }, {})
      } else {
        wx.showToast({
          title: '上传失败',
          icon: 'none'
        })
      }
    },
    requestDidFaild: (res) => {
      wx.hideLoading()
      console.log(res)
      wx.showToast({
        title: '网络走神了，请稍候再试哦',
        icon: 'none'
      })
    }
  }
})
